package com.wyz.saas.common.map.manager

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.atom.module.mvvm.base.AbstractViewModelFactory
import com.atom.module.mvvm.base.EmptyModel
import com.atom.module.mvvm.wight.recycler.base.BaseRecyclerAdapter
import com.wyz.saas.api.map.MapOverlayManager
import com.wyz.saas.common.R
import com.wyz.saas.common.base.AbstractApiToolbarFragment
import com.wyz.saas.common.databinding.CellMapOverlayerItemBinding
import com.wyz.saas.common.databinding.FragmentMapOverlayerManagerBinding
import com.wyz.saas.common.impl.EmptyApiModel
import com.wyz.saas.common.utils.otherwise
import com.wyz.saas.common.utils.yes

class MapOverlayManagerFragment :
    AbstractApiToolbarFragment<FragmentMapOverlayerManagerBinding, MapOverlayManagerViewModel>() {

    private var adapter: BaseRecyclerAdapter<MapOverlayManager.MapOverlay, CellMapOverlayerItemBinding>? =
        null

    override fun initData(view: View?) {
        super.initData(view)
        view?.also { root->
            val findViewById = root.findViewById<RecyclerView>(R.id.map_overlayer_list)
            findViewById.layoutManager = LinearLayoutManager(context)
            findViewById.adapter = object : BaseRecyclerAdapter<MapOverlayManager.MapOverlay, CellMapOverlayerItemBinding>() {
                override fun getItemViewType(
                    position: Int,
                    data: MapOverlayManager.MapOverlay
                ): Int {
                    return R.layout.cell_map_overlayer_item
                }

                override fun onCreateViewHolder(
                    binding: CellMapOverlayerItemBinding,
                    viewType: Int
                ): ViewHolder<MapOverlayManager.MapOverlay, CellMapOverlayerItemBinding> {
                    return MapViewHolder(binding)
                }

            }.also {
                adapter = it
            }
        }
    }

    override fun initToolbar(toolbar: Toolbar?) {
        super.initToolbar(toolbar)
        toolbar?.title = getString(R.string.map_overlayer_manager_title)
    }

    override fun onFirst() {
        super.onFirst()
        contentView?.post {
            apiContext().getImpl(MapOverlayManager::class.java)?.also { manager ->
                adapter?.addAll(manager.overlayers())
            }
        }
    }

    inner class MapViewHolder(viewBinding: CellMapOverlayerItemBinding) :
        BaseRecyclerAdapter.ViewHolder<MapOverlayManager.MapOverlay, CellMapOverlayerItemBinding>(
            viewBinding
        ) {

        var icon: ImageView? = null

        var enabled: ImageView? = null

        var label: TextView? = null

        init {
            icon = itemView.findViewById(R.id.map_overlayer_item_icon)
            enabled = itemView.findViewById(R.id.map_overlayer_item_enable)
            label = itemView.findViewById(R.id.map_overlayer_item_label)
        }

        override fun onBind(data: MapOverlayManager.MapOverlay) {
            icon?.setImageBitmap(data.icon())
            label?.text = data.display()
            Log.e("MapOverlayManager", data.display())
            enabled?.setImageBitmap(data.isEnabled().yes {
                apiContext().getBitmap(R.mipmap.icon_map_overlayer_show)
            }.otherwise {
                apiContext().getBitmap(R.mipmap.icon_map_overlayer_hide)
            })
            enabled?.setOnClickListener {
                data.enable(!data.isEnabled())
                updateData(data)
            }
        }
    }

    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.fragment_map_overlayer_manager

    override fun initViewModel(): MapOverlayManagerViewModel {
        val factory = object : AbstractViewModelFactory() {
            override fun <T : ViewModel?> create(modelClass: Class<T>): T {
                if (modelClass.isAssignableFrom(MapOverlayManagerViewModel::class.java)) {
                    return MapOverlayManagerViewModel(EmptyApiModel()) as T
                }
                return modelClass.newInstance()
            }
        }
        return ViewModelProvider(this, factory).get(MapOverlayManagerViewModel::class.java)
    }
}